package com.storage.web.controller.system;

import com.storage.common.annotation.Log;
import com.storage.common.core.controller.BaseController;
import com.storage.common.core.domain.AjaxResult;
import com.storage.common.core.domain.ServiceResponse;
import com.storage.common.core.page.TableDataInfo;
import com.storage.common.enums.BusinessType;
import com.storage.framework.util.ShiroUtils;
import com.storage.framework.web.OperationContext;
import com.storage.framework.web.enums.RequestOperation;
import com.storage.system.domain.StorageCategory;
import com.storage.system.service.IStorageCategoryService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 分类 信息操作处理
 *
 * @author storage
 * @date 2019-08-18
 */
@Api(tags = "分类相关接口")
@RestController
public class StorageCategoryController extends BaseController {

    @Autowired
    private IStorageCategoryService storageCategoryService;

    /**
     * 查询分类列表
     */
    @ApiOperation(value = "查询分类列表")
    @GetMapping("/system/storageCategory/selectList")
    @ResponseBody
    public AjaxResult list(@RequestOperation OperationContext context,
                           StorageCategory storageCategory) {
        ServiceResponse<List<StorageCategory>> response = storageCategoryService.selectStorageCategorySelectList(context.getStorageUser(), storageCategory);
        if (!response.isSuccess()) {
            AjaxResult.error(response.getError());
        }
        return AjaxResult.success(response.getData());
    }

    /**
     * 查询分类列表
     */
    @ApiOperation(value = "查询分类列表")
    @GetMapping("/system/storageCategory/list")
    @ResponseBody
    public TableDataInfo list(@RequestOperation OperationContext context,
                              StorageCategory storageCategory, Integer pageNum, Integer pageSize) {
        ServiceResponse<TableDataInfo> response = storageCategoryService.selectStorageCategoryList(context.getStorageUser(), storageCategory, pageNum, pageSize);
        return response.getData();
    }

    /**
     * 新增保存分类
     */
    @ApiOperation(value = "新增保存分类")
    @Log(title = "分类", businessType = BusinessType.INSERT)
    @PostMapping("/system/storageCategory/add")
    @ResponseBody
    public AjaxResult addSave(@RequestOperation OperationContext context,
                              @RequestBody StorageCategory storageCategory) {
        storageCategory.setCreateUserId(ShiroUtils.getUserId());
        return storageCategoryService.insertStorageCategory(context.getStorageUser(), storageCategory);
    }

    /**
     * 修改保存分类
     */
    @ApiOperation(value = "修改保存分类")
    @Log(title = "分类", businessType = BusinessType.UPDATE)
    @PostMapping("/system/storageCategory/edit")
    @ResponseBody
    public AjaxResult editSave(@RequestOperation OperationContext context,
                               @RequestBody StorageCategory storageCategory) {
        storageCategory.setCreateUserId(ShiroUtils.getUserId());
        return storageCategoryService.updateStorageCategory(context.getStorageUser(), storageCategory);
    }

    /**
     * 删除分类
     */
    @ApiOperation(value = "删除分类")
    @Log(title = "分类", businessType = BusinessType.DELETE)
    @GetMapping("/system/storageCategory/remove")
    @ResponseBody
    public AjaxResult remove(@RequestParam("ids") String ids) {
        return toAjax(storageCategoryService.deleteStorageCategoryByIds(ids));
    }

}
